package com.team.arms.yuding.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.core.page.TableDataInfo;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.yuding.domain.DoubleQualified;
import com.team.arms.yuding.mapper.DoubleQualifiedMapper;
import com.team.arms.yuding.service.DoubleQualifiedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yjh
 * @create 2022-05-06-10:23
 * @description:
 */
@Api( tags = "双合格")
@RestController
@RequestMapping("/reservesoldiers/doublequakified")
public class DoubleQualifiedController extends BaseController {

    @Autowired
    private DoubleQualifiedService doubleQualifiedService;

    @Autowired
    private DoubleQualifiedMapper doubleQualifiedMapper;

    @ApiOperation("查询所有双合格信息")
    @GetMapping("/select")
    public AjaxResult selectPage(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                 @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<DoubleQualified> list = doubleQualifiedService.list();
        PageInfo<DoubleQualified> pageInfo = new PageInfo<DoubleQualified>(list);
        PageVO<DoubleQualified> doubleQualifiedPageVO = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, doubleQualifiedPageVO);
        doubleQualifiedPageVO.setResult(pageInfo.getList());
        doubleQualifiedPageVO.setPage(pageNum);
        return AjaxResult.success(doubleQualifiedPageVO);

    }

    @ApiOperation("按条件查询双合格信息")
    @PostMapping("/selectDoubleQualified")
    public TableDataInfo selectDoubleQualified(@RequestBody(required = false) DoubleQualified doubleQualified){
        startPage();
        List<DoubleQualified> list = doubleQualifiedService.selectDoubleQualified(doubleQualified);
        return getDataTable(list);
    }

    @ApiOperation("根据id查询双合格信息")
    @GetMapping("/selectById")
    public AjaxResult selectById(@RequestParam(value = "double_qualified_id")String id){
        return AjaxResult.success(doubleQualifiedService.getById(id));
    }

    /**
     *
     * @param field
     * @param content
     * @return
     */
//    @ApiOperation("根据对应字段查询双合格信息")
//    @GetMapping("/selectByFile")
//    public AjaxResult selectByName( @ApiParam("查询的字段")@RequestParam(value = "field") String field ,
//                                    @ApiParam("查询的内容")@RequestParam(value = "content")String content){
//        QueryWrapper<DoubleQualified> queryWrapper = new QueryWrapper<>();
//        if (field.equals("姓名")) {
//            queryWrapper.like("name", content);
//        }
//        if (field.equals("性别")) {
//            queryWrapper.eq("sex", content);
//        }
//        if (field.equals("年度")) {
//            queryWrapper.eq("year", content);
//        }
//        if (field.equals("批次")) {
//            queryWrapper.eq("batch", content);
//        }
//        if (field.equals("网登编号")) {
//            queryWrapper.like("login_number", content);
//        }
//        if (field.equals("年龄")) {
//            queryWrapper.eq("age", content);
//        }
//        if (field.equals("民族")) {
//            queryWrapper.eq("nation", content);
//        }
//        if (field.equals("户籍类型")) {
//            queryWrapper.like("household_type", content);
//        }
//        if (field.equals("学历")) {
//            queryWrapper.eq("cultureral_program", content);
//        }
//        if (field.equals("登记报名时间")) {
//            queryWrapper.eq("registration_time", content);
//        }
//
//        return AjaxResult.success(doubleQualifiedMapper.selectList(queryWrapper));
//    }
    @ApiOperation("根据对应字段查询双合格信息")
    @GetMapping("/selectByFile")
    public AjaxResult selectByField(@ApiParam("查询的字段")@RequestParam(value = "field") String field ,
                                   @ApiParam("查询的内容")@RequestParam(value = "content")String content){
        return AjaxResult.success(doubleQualifiedMapper.selectByFiled(field,content));
    }

    @ApiOperation("修改双合格信息")
    @Log(title = "双合格信息", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult updateInformation(
            @RequestParam(value = "double_qualified_id")String id,
            @RequestBody DoubleQualified doubleQualified){
        QueryWrapper<DoubleQualified> wrapper = new QueryWrapper();
        wrapper.eq("double_qualified_id",id);
        DoubleQualified qualifiedServiceById = doubleQualifiedService.getById(id);
        return toAjax(doubleQualifiedService.update(doubleQualified,wrapper));
    }

    /**
     * 新增双合格信息
     */
    @ApiOperation("新增双合格信息")
    @PostMapping("/install")
    public AjaxResult installInformation(@RequestBody DoubleQualified doubleQualified){
        return toAjax(doubleQualifiedService.installAll(doubleQualified));
    }

    /**
     * 批量删除双合格信息
     * @param ids
     * @return
     */
    @ApiOperation("批量删除双合格信息")
    @DeleteMapping("/delete")
    public AjaxResult deleteInformation(@RequestBody String[] ids){
        ArrayList<String> list = new ArrayList<String>();
        for(String id : ids){
            list.add(id);
        }
        return toAjax(doubleQualifiedService.removeBatchByIds(list));
    }

    /**
     * 删除双合格信息
     * @param id
     * @return
     */
    @ApiOperation("删除双合格信息")
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(String id){
        return toAjax(doubleQualifiedService.removeById(id));
    }

    /**
     * 导出双合格信息
     */
    @ApiOperation("导出双合格信息")
    @Log(title = "双合格信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码
        String fileName = "test1";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<DoubleQualified> doubleQualifieds = doubleQualifiedService.list();//调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), DoubleQualified.class).sheet("双合格信息")
                    .doWrite(doubleQualifieds);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
